
function loading(title,discription,name){
	var name = name ? name : 'loading';
	var tite = title ? title : '';
	var desc = discription ? discription : '';
	$('body').loading({
		loadingWidth:240,
		title:tite,
		name:'loading',
		discription:desc,
		direction:'column',
		type:'origin',
		originBg:'#71EA71',
		originDivWidth:40,
		originDivHeight:40,
		originWidth:6,
		originHeight:6,
		smallLoading:false,
		loadingMaskBg:'rgba(0,0,0,0.2)'
	});
}

function rmLoading(name){
	var name = name ? name : 'loading';
	removeLoading(name);
}

// 侧边栏
function toggleSideBar(_this){
	var b = $("#sidebar-collapse")[0];
	var w = $("#cl-wrapper");
	var s = $(".cl-sidebar");
	
	if(w.hasClass("sb-collapsed")){
	  $(".fa",b).addClass("icon-left").removeClass("icon-right");
	  w.removeClass("sb-collapsed");
	}else{
	  $(".fa",b).removeClass("icon-left").addClass("icon-right");
	  w.addClass("sb-collapsed");
	}
	//updateHeight();
}

$(document).ready(function(){
	$(".cl-vnavigation li ul").each(function(){
        $(this).parent().addClass("parent");
    });
    $(".cl-vnavigation li ul").each(function(){
        $(this).parent().addClass("parent");
    });
    $(".cl-vnavigation").delegate(".parent > a","click",function(e){
        $(".cl-vnavigation .parent.open > ul").not($(this).parent().find("ul")).slideUp(300, 'swing',function(){
           $(this).parent().removeClass("open");
        });
        
        var ul = $(this).parent().find("ul");
        ul.slideToggle(300, 'swing', function () {
          var p = $(this).parent();
          if(p.hasClass("open")){
            p.removeClass("open");
          }else{
            p.addClass("open");
          }
          //var menuH = $("#cl-wrapper .menu-space .content").height();
          // var height = ($(document).height() < $(window).height())?$(window).height():menuH;
          //updateHeight();
         $("#cl-wrapper .nscroller").nanoScroller({ preventPageScrolling: true });
        });
        e.preventDefault();
    });
    var tool = $("<div id='sub-menu-nav' style='position:fixed;z-index:9999;'></div>");
        
        function showMenu(_this, e){
          if(($("#cl-wrapper").hasClass("sb-collapsed") || ($(window).width() > 755 && $(window).width() < 963)) && $("ul",_this).length > 0){   
            $(_this).removeClass("ocult");
            var menu = $("ul",_this);
            if(!$(".dropdown-header",_this).length){
              var head = '<li class="dropdown-header">' +  $(_this).children().html()  + "</li>" ;
              menu.prepend(head);
            }
            
            tool.appendTo("body");
            var top = ($(_this).offset().top + 8) - $(window).scrollTop();
            var left = $(_this).width();
            
            tool.css({
              'top': top,
              'left': left + 8
            });
            tool.html('<ul class="sub-menu">' + menu.html() + '</ul>');
            tool.show();
            
            menu.css('top', top);
          }else{
            tool.hide();
          }
        }

        $(".cl-vnavigation li").hover(function(e){
          showMenu(this, e);
        },function(e){
          tool.removeClass("over");
          setTimeout(function(){
            if(!tool.hasClass("over") && !$(".cl-vnavigation li:hover").length > 0){
              tool.hide();
            }
          },500);
        });
        
        tool.hover(function(e){
          $(this).addClass("over");
        },function(){
          $(this).removeClass("over");
          tool.fadeOut("fast");
        });
        
        
        $(document).click(function(){
          tool.hide();
        });
        $(document).on('touchstart click', function(e){
          tool.fadeOut("fast");
        });
        
        tool.click(function(e){
          e.stopPropagation();
        });
     
        $(".cl-vnavigation li").click(function(e){
          if((($("#cl-wrapper").hasClass("sb-collapsed") || ($(window).width() > 755 && $(window).width() < 963)) && $("ul",this).length > 0) && !($(window).width() < 755)){
            showMenu(this, e);
            e.stopPropagation();
          }
        });
        
        $(".cl-vnavigation li").on('touchstart click', function(){
          //alert($(window).width());
        });
        if($("#cl-wrapper").hasClass("fixed-menu")){
          var scroll =  $("#cl-wrapper .menu-space");
          scroll.addClass("nano nscroller");

          function update_height(){
            var button = $("#cl-wrapper .collapse-button");
            var collapseH = button.outerHeight();
            var navH = $("#head-nav").height();
            var height = $(window).height() - ((button.is(":visible"))?collapseH:0) - navH;
            scroll.css("height",height);
            $("#cl-wrapper .nscroller").nanoScroller({ preventPageScrolling: true });
          }
          
          $(window).resize(function() {
            update_height();
          });    
              
          update_height();
          $("#cl-wrapper .nscroller").nanoScroller({ preventPageScrolling: true });
          
        }
    	  // 侧边栏
    	  $("#sidebar-collapse").click(function(){
          toggleSideBar();
      	});
        $(".cl-toggle").click(function(e){
            var ul = $(".cl-vnavigation");
            ul.slideToggle(300, 'swing', function () {
            });
            e.preventDefault();
        });
});

